'''
Call SuckRunner with arguments parsed from the command line interface.
'''

import argparse
from suckrunner import SuckRunner

# TODO handle CTRL-C

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Retrieve internet radio streams and save them as .pls '
                    'or/and .plsx (Xtreamer) files.')
    parser.add_argument('seeds',
                        metavar='URL',
                        type=str,
                        nargs='+',
                        help='URLs of web radio stations')
    parser.add_argument('-pls',
                        metavar='DIR',
                        type=str,
                        nargs=1,
                        default='.',
                        help='write one .pls file for each station to DIR')
    parser.add_argument('-plsx',
                        metavar='DIR',
                        type=str,
                        nargs=1,
                        help='write one .plsx file for each stream to DIR/URL')
    parser.add_argument('-d', '--depth',
                        metavar='LEVEL',
                        type=int,
                        nargs=1,
                        default=[1],
                        help='crawl depth (number of links to follow)')

    args = parser.parse_args()

    # TODO disable logging to file by default
    # TODO seems like -plsx arg is not honored

    suck_calls = []
    for seed in args.seeds:
        suck = { 'seed': seed,
                 'writers': [] }
        if args.pls:
            assert len(args.pls) == 1
            suck['writers'].append(('writepls', { 'dir': args.pls[0] }))
        if args.plsx:
            assert len(args.plsx) == 1
            suck['writers'].append(('writeplsx', { 'dir': args.pls[0] }))
        if args.depth:
            assert len(args.depth) == 1
            suck['depth'] = args.depth[0]  # TODO [0] everywhere seems broken
        suck_calls.append(suck)

    runner = SuckRunner()
    runner.run(suck_calls)

